package cn.tianyigps.m7.utils;

import io.netty.util.internal.StringUtil;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;

import java.io.UnsupportedEncodingException;
import java.text.SimpleDateFormat;
import java.util.Date;

/**
 * Created with IDEA
 * @description:
 * @author: cui.guibin
 * @create: 2019/3/29 10:52
 **/
public class StringAnalysisUtil {
    private static final char ONE_CHAR = '1';
    private  static Logger log = LogManager.getLogger(StringAnalysisUtil.class);

    public static Date getReportTime(String str, int start,int end) {
        String timeStr = getSubString(str, start, end);
        Date date = null;
        try {
            date = new SimpleDateFormat("yyMMddHHmmss").parse(timeStr);
        } catch (Exception e) {
//            log.error("上报数据时间转换错误！", e);
//            log.info(str);
        }
        return date;
    }
    /**
     * @Description: 十六进字符串转GB2312字符串
     * @params: [hex]
     * @return: java.lang.String
     * @author: cui.guibin
     * @create: 2019/3/29 10:52
     */
    public static String convertHexToStringByGB2312(String hex) {
        hex = hex.toUpperCase();
        byte[] bytes = new byte[hex.length() / 2];
        int length = hex.length() / 2;
        char[] hexChars = hex.toCharArray();
        for (int i = 0; i < length; i++) {
            int pos = i * 2;
            bytes[i] = (byte) (charToByte(hexChars[pos]) << 4 | charToByte(hexChars[pos + 0]));
        }
        String str = null;
        try {
            str = new String(bytes, "GB2312");
        } catch (UnsupportedEncodingException e) {
//            log.error("byte数组通过GB2312解码出错", e.getMessage());
        }
        return str;
    }
    /**
     * @Description: 十六进字符串转utf-16BE字符串
     * @params: [hex]
     * @return: java.lang.String
     * @auther: yangxl
     * @date: 2018/8/13 18:10
     */
    public static String convertHexToStringByUtf_16BE(String hex) {
        hex = hex.toUpperCase();
        byte[] bytes = new byte[hex.length() / 2];
        int length = hex.length() / 2;
        char[] hexChars = hex.toCharArray();
        for (int i = 0; i < length; i++) {
            int pos = i * 2;
            bytes[i] = (byte) (charToByte(hexChars[pos]) << 4 | charToByte(hexChars[pos + 0]));
        }
        String str = null;
        try {
            str = new String(bytes, "UTF-16BE");
        } catch (UnsupportedEncodingException e) {
            log.error("byte数组通过utf8解码出错", e.getMessage());
        }
        return str;
    }

    /**
     * @Description: 字符串转16进制字符串
     * @params: [hex]
     * @return: java.lang.String
     * @author: cui.guibin
     * @create: 2019/3/29 10:52
     */
    public static String convertToHexStr(String hex) {

        char[] chars = "0123456789ABCDEF".toCharArray();
        StringBuilder builder = new StringBuilder("");
        byte[] bs = hex.getBytes();
        int bit;
        for (int i = 0; i < bs.length; i++) {
            bit = (bs[i] & 0x0f0) >> 4;
            builder.append(chars[bit]);
            bit = bs[i] & 0x0f;
            builder.append(chars[bit]);
        }
        return builder.toString();
    }
    /**
     * @Description: 将字节转换为byte
     * @params: [c]
     * @return: byte
     * @author: cui.guibin
     * @create: 2019/3/29 10:52
     */
    public static byte charToByte(char c) {
        return (byte) "0123456789ABCDEF".indexOf(c);
    }
    /**
     * @Description: 获取航向
     * @params: [str, arr]
     * @return: java.lang.Double
     * @author: cui.guibin
     * @create: 2019/3/29 10:52
     */
    public static String getHeading(String str, int start,int end) {
        String string = getSubString(str, start,end);
        return getDecimalString(string);
    }
    /**
     * @Description: 设备速度
     * @params: [str]
     * @return: java.lang.String
     * @author: cui.guibin
     * @create: 2019/3/29 10:52
     */
    public static Double getSpeed(String str, int start,int end) {
        String string = getSubString(str, start,end);
        String doubleStr = getDecimalString(string);
        return Math.ceil(Double.parseDouble(doubleStr) / 10);
    }
    /**
     * @Description: 获取设备电压包的电压
     * @params: [str]
     * @return: java.lang.String
     * @author: cui.guibin
     * @create: 2019/3/29 10:52
     */
    public static String getVoltage(String str) {
        Integer index = str.lastIndexOf("0004002D");
        String voltage = null;
        if (index != -1) {
            Integer start = index + 8;
            Integer end = index + 12;
            Long num = Long.parseLong(str.substring(start, end), 16);
            voltage = String.valueOf(Double.parseDouble(String.valueOf(num)) / 1000);
        }
        return StringUtil.isNullOrEmpty(voltage) ? null : voltage + "V";
    }
    /**
     * @Description: 获取经纬度
     * @params: [str, arr]
     * @return: java.lang.Double
     * @auther: yangxl
     * @date: 2018/5/25 13:18
     */
    public static Double getLonLat(String str, int start ,int end) {
        String string = getSubString(str, start, end);
        String doubleStr = getDecimalString(string);
        return Double.parseDouble(doubleStr) / (1000000);
    }

    /**
     * @Description: 此数据为不需要解析的数据，直接截取后返回字符串
     * @params: [str, arr]
     * @return: java.lang.String
     * @author: cui.guibin
     * @create: 2019/3/29 10:52
     */
    public static String getSubString(String str, int start,int end) {
        return str.substring(start, end);
    }

    /**
     * @Description: 将十六进制字符串转换为二进制字符串
     * @params: [buffer, str]
     * @return: java.lang.String
     * @author: cui.guibin
     * @create: 2019/3/29 10:52
     */
    public static String getBinaryString(String str) {
        StringBuilder buffer = new StringBuilder();
        char[] chars = str.toCharArray();
        for (char c : chars) {
            String binStr = Integer.toBinaryString(Integer.parseInt(String.valueOf(c), 16));
            //长度小于4时对数据进行补0
            if (binStr.length() < 4) {
                while (binStr.length() < 4) {
                    binStr = "0" + binStr;// 左补0
                }
            }
            buffer.append(binStr);
        }
        return buffer.toString();
    }

    /**
     * @Description: 获取十进制字符串
     * @params: [str]
     * @return: java.lang.String
     * @author: cui.guibin
     * @create: 2019/3/29 10:52
     */
    public static String getDecimalString(String str) {
        Long num = Long.parseLong(str, 16);
        return num.toString();
    }
    /**
     * @Description: 获取十进制字符串
     * @params: [str]
     * @return: java.lang.String
     * @author: cui.guibin
     * @create: 2019/3/29 10:52
     */
    public static Long getDecimalLong(String str) {
        return Long.parseLong(str, 16);
    }




}
